/*
 * Copyright 2008,2009,2010 Daniel Freitas
 * 
 * This file is part of DMF Generic DAO.
 * 
 * DMF Generic DAO is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General
 * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * 
 * DMF Generic DAO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with DMF Generic DAO. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dmf.genericdao.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;

import com.googlecode.dmf.genericdao.GenericDao;
import com.googlecode.dmf.genericdao.GenericlessGenericDao;

/**
 * Hibernate default implementation for the {@link GenericDao} interface.
 * 
 * @author <a href="mailto:daniel.mfreitas@gmail.com">Daniel Freitas</a>
 */
public class HibernateGenericDao<T> implements GenericlessGenericDao<T> {

    private final SessionFactory sessionFactory;
    private Class<T> injectedEntityClass;

    /**
     * Constructs a new {@link HibernateGenericDao}. This version of the constructor is meant to be used
     * 
     * @param sessionFactory
     *            The hibernate session factory used to retrieve {@link Session}s from.
     */
    public HibernateGenericDao(final SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    /**
     * Constructs a new {@link HibernateGenericDao}.
     * 
     * @param
     * 
     * @param sessionFactory
     *            The hibernate session factory used to retrieve {@link Session}s from.
     */
    public HibernateGenericDao(final Class<T> entityClass, final SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.injectedEntityClass = entityClass;
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.GenericDao#findAll()
     */
    @Override
    public List<T> findAll() {
        return findByCriteria(injectedEntityClass);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Object> findAll(final Class<?> entityClass) {
        return (List<Object>) findByCriteria((Class<T>) entityClass);
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.GenericDao#findByExample(java.lang.Object, java.lang.String[])
     */
    @Override
    @SuppressWarnings("unchecked")
    public List<T> findByExample(final T exampleObject, final String... excludeProperty) {
        Example example = Example.create(exampleObject);

        for (String property : excludeProperty) {
            example.excludeProperty(property);
        }

        return findByCriteria((Class<T>) exampleObject.getClass(), example);
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.GenericDao#findById(java.io.Serializable)
     */
    @Override
    @SuppressWarnings("unchecked")
    public <V extends Serializable> T findById(final V id) {
        return (T) sessionFactory.getCurrentSession().get(injectedEntityClass, id);
    }

    @Override
    public Object findById(final Class<?> entityClass, final Serializable id) {
        return sessionFactory.getCurrentSession().get(entityClass, id);
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.GenericDao#makePersistent(java.lang.Object)
     */
    @Override
    public T makePersistent(final T o) {
        sessionFactory.getCurrentSession().saveOrUpdate(o);
        return o;
    }

    /**
     * 
     * {@inheritDoc}
     * 
     * @see com.googlecode.dmf.genericdao.GenericDao#makeTransient(java.lang.Object)
     */
    @Override
    public void makeTransient(final T o) {
        sessionFactory.getCurrentSession().delete(o);
    }

    public Class<T> getInjectedEntityClass() {
        return injectedEntityClass;
    }

    // Extracted methods
    // -----------------------------------------------------------------------------------------------------------------
    @SuppressWarnings("unchecked")
    private List<T> findByCriteria(final Class<T> entityClass, final Criterion... criterion) {
        Criteria criteria = sessionFactory.getCurrentSession().createCriteria(entityClass);

        for (Criterion c : criterion) {
            criteria.add(c);
        }

        List<T> result = criteria.list();

        return result;
    }
}
